package com.juneng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.juneng.common.Condition;
import com.juneng.dto.OrderRefundResponse;
import com.juneng.entity.OrderMsg;
import com.juneng.mapper.OrderMsgMapper;
import com.juneng.utils.BeanCopyUtils;
import com.juneng.entity.OrderRefund;
import com.juneng.mapper.OrderRefundMapper;
import com.juneng.service.IOrderRefundService;
import com.juneng.dto.OrderRefundDto;
import com.juneng.dto.OrderRefundQueryDto;
import com.juneng.vo.OrderRefundVo;
import org.springframework.stereotype.Service;
import lombok.AllArgsConstructor;
import java.util.List;
/**
 * 订单商品退款 服务实现类
 *
 * @author zds
 * @since 2021-08-04
 */
@Service
@AllArgsConstructor
public class OrderRefundServiceImpl implements IOrderRefundService {

    protected OrderRefundMapper orderRefundMapper;

    protected OrderMsgMapper orderMsgMapper;

    @Override
    public IPage<OrderRefundVo> page(OrderRefundQueryDto dto) {
        IPage<OrderRefundVo> page = Condition.getPage(dto);
        IPage<OrderRefundVo> page1 = orderRefundMapper.page(page, dto);
        List<OrderRefundVo> records = page1.getRecords();
        for (OrderRefundVo record : records) {
            List<OrderMsg> orderMsgs = orderMsgMapper.selectList(new LambdaQueryWrapper<OrderMsg>().eq(OrderMsg::getInternalOrder, record.getInternalOrder()));
            record.setProduct(orderMsgs);
        }
        return page1;
    }

    @Override
    public List<OrderRefund> list(OrderRefundQueryDto dto) {
        QueryWrapper<OrderRefund> queryWrapper = new QueryWrapper<>(BeanCopyUtils.copy(dto,OrderRefund.class));
        return orderRefundMapper.selectList(queryWrapper);
    }

    @Override
    public Integer save(OrderRefundDto dto) {
        return orderRefundMapper.insert(BeanCopyUtils.copy(dto,OrderRefund.class));
    }

    @Override
    public Integer updateById(OrderRefundDto dto) {
        QueryWrapper<OrderRefund> wrapper = new QueryWrapper<>();
        wrapper.eq("out_refund_no",dto.getOutRefundNo());
      //  int update = orderRefundMapper.update(BeanCopyUtils.copy(dto, OrderRefund.class), wrapper);
        return orderRefundMapper.update(BeanCopyUtils.copy(dto, OrderRefund.class), wrapper);
    }

    @Override
    public Integer deleteLogic(List<Integer> toIntList) {
        return orderRefundMapper.deleteBatchIds(toIntList);
    }

    @Override
    public OrderRefundResponse getById(Long id) {

        return orderRefundMapper.getOrderRefund(id);
    }

    @Override
    public Integer updateOrderRefund(OrderRefund orderRefund) {
        QueryWrapper<OrderRefund> wrapper = new QueryWrapper<>();
        wrapper.eq("out_refund_no",orderRefund.getOutRefundNo());
        return orderRefundMapper.update(orderRefund,wrapper);
    }

    @Override
    public List<OrderRefundVo> getOrderRefundList(OrderRefundQueryDto dto) {
        List<OrderRefundVo> orderRefundList = orderRefundMapper.getOrderRefundList(dto);
        for (OrderRefundVo orderRefundVo : orderRefundList) {
            List<OrderMsg> orderMsgs = orderMsgMapper.selectList(new LambdaQueryWrapper<OrderMsg>().eq(OrderMsg::getInternalOrder, orderRefundVo.getInternalOrder()));
            orderRefundVo.setProduct(orderMsgs);
        }
        return orderRefundList;
    }
}
